/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/memory
// Copyright( c) 2013.  All Rights Reserved
//
// File:		AEDefaultMemoryConfig.h
// Author:		Gianluca Belardelli
// Date:		20/03/2014
//
/////////////////////////////////////////////////////////////////////////
#ifndef _AEDEFAULTMEMORYCONFIG_H_
#define _AEDEFAULTMEMORYCONFIG_H_

#define THREAD_MAX		64

class AEDefaultMemoryConfig : public AEMemoryConfig
{
// Members
public:
	struct AEThreadData
	{
		AE_DECLARE_NONVIRTUAL_CLASS_ALLOCATOR( TOBEDEFINED, AEThreadData );
	
		//hkThreadMemory m_heapThreadMemory; ///< A thread local wrapper around heap memory to speed allocs/free up
		AEMemoryManager *m_lpThreadMemManager; ///< The thread local memory router
		const char		*m_lpThreadName; ///< The name of the thread
		AEBOOL32		m_bInUse; ///< Set to true if this ThreadData is used by a thread

		AEThreadData( void ) : m_lpThreadName( AENULL ), m_bInUse( AEFALSE ) {}
	};

protected:
	AEMemoryAllocator	*m_lpSystemAllocator;
	AEMemoryAllocator	*m_lpHeapAllocator;
	AEMemoryManager		*m_lpMainMemMgr;

	AEThreadData m_tdThreadData[THREAD_MAX];
	mutable AECriticalSection m_csThreadDataLock;

public:
	AE_DLLIMPEXP AEDefaultMemoryConfig( AEMemoryAllocator *lpSystemAllocator, AEMemoryAllocator *lpHeapAllocator );
	AE_DLLIMPEXP ~AEDefaultMemoryConfig( void );

	AE_DLLIMPEXP virtual AEMemoryManager *MainInit( const AEMemoryConfigInfo &mcInfo );

	/// Shut down the memory system.
	/// Some implementations will return HK_FAILURE if leaks are detected.
	AE_DLLIMPEXP virtual AERESULT MainQuit( void );

	/// Method for threads to construct their local instances.
	AE_DLLIMPEXP virtual void ThreadMemInit( AEMemoryManager &refMemMgr, const char *lpName );

	/// Method for threads to deinitialize their local instances.
	AE_DLLIMPEXP virtual void ThreadMemQuit( AEMemoryManager &refMemMgr );
};

#endif // _AEDEFAULTMEMORYCONFIG_H_
